//
//  AFNetworkingTool.m
//  AFNnetworking
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 mjh. All rights reserved.
//

#import "AFNetworkingTool.h"

@implementation AFNetworkingTool

#pragma mark - 判断网络状态
+ (void)netWorkState{
    Reachability *reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:{
            // 没有网络连接
            [AFNIsnetWorkingTool sharedIsNetworking].isWiFi = NO;
            NSLog(@"notReachable");
            break;
        }

        case ReachableViaWWAN:{
            // 使用3G网络
            [AFNIsnetWorkingTool sharedIsNetworking].isWiFi = YES;
            NSLog(@"3G");
        }break;
        case ReachableViaWiFi:{
            // 使用WiFi网络
            [AFNIsnetWorkingTool sharedIsNetworking].isWiFi = YES;
            NSLog(@"Wifi");
            break;
        }
    }
}



#pragma mark - GET请求
+ (void)getUrl:(NSString *)url hudSuperView:(id)hudSuperView result:(MyResult)result success:(void (^)(id))success failure:(void (^)(NSError *))failure{
//    //判断网络
    [self netWorkState];
    //加菊花
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:hudSuperView animated:YES];
    hud.labelText = @"Loading..";
    if ([AFNIsnetWorkingTool sharedIsNetworking].isWiFi) {
    
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        //超时
        [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        manager.requestSerializer.timeoutInterval = 10.f;
        [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
        // 如果Response显示的内容在线JSON没有显示的话, 就用下面的方法
        // 特别屌的方法: 把抓包工具中的Request下面的:User-Agent给复制到参数二上  ,  把User-Agent对应的给赋值到参数1上
        [manager.requestSerializer setValue:@"BreadTrip/6.0.0/zh (iPhone7,2; iPhone OS8.3; zh-Hans zh_CN) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
        
        //网络请求返回值的数据类型
        switch (result) {
            case Data:
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                break;
            case XML:
                manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
                break;
            case JSON:
                manager.responseSerializer = [AFJSONResponseSerializer serializer];
                break;
            default:
                break;
        }

        //设置网络请求返回值所支持的参数类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/javascript", nil]];
        // 有汉字转码
        NSString *url_string = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:url]];

        [manager GET:url_string parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            if (responseObject) {
                [hud removeFromSuperview];
                success(responseObject);
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:LanguageTool(@"警告") message:LanguageTool(@"网络连接错误") delegate:nil cancelButtonTitle:LanguageTool(@"确定") otherButtonTitles:nil];
            [alert show];
            [hud removeFromSuperview];
            [alert dismissWithClickedButtonIndex:0 animated:YES];
            NSLog(@"错误++++++++%@",error);
        }];
    }else{
        [hud removeFromSuperview];
    }
    
}



#pragma mark - POST请求
+ (void)postUrl:(NSString *)url body:(id)body hudSuperView:(id)hudSuperView result:(MyResult)result success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    
    //网络判断
    [self netWorkState];
    //菊花
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:hudSuperView animated:YES];
    hud.labelText = @"Loading..";
    
    if ([AFNIsnetWorkingTool sharedIsNetworking].isWiFi) {
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        //超时
        [manager.requestSerializer willChangeValueForKey:@"timeoutInterval"];
        manager.requestSerializer.timeoutInterval = 10.f;
        [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"];
        
        //网络请求返回值的数据类型
        switch (result) {
            case Data:
                manager.responseSerializer = [AFHTTPResponseSerializer serializer];
                break;
            case XML:
                manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
                break;
            case JSON:
                manager.responseSerializer = [AFJSONResponseSerializer serializer];
                break;
            default:
                break;
        }
    
        //设置网络请求返回值所支持的参数类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain",@"application/javascript", nil]];
        // 有汉字转码
        NSString *url_string = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:url]];
        
        [manager POST:url_string parameters:body success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            if (responseObject) {
                [hud removeFromSuperview];
                success(responseObject);
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if ([AFNIsnetWorkingTool sharedIsNetworking].isWiFi == YES) {
//                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:LanguageTool(@"警告") message:LanguageTool(@"网络连接超时,请检查网络")  delegate:nil cancelButtonTitle:LanguageTool(@"确定") otherButtonTitles:nil];
//                [alert show];
                [hud removeFromSuperview];
            }
            failure(error);
            NSLog(@"错误++++++++%@",error);
        }];
    }else{
        [hud removeFromSuperview];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:LanguageTool(@"警告") message:LanguageTool(@"未连接网络") delegate:nil cancelButtonTitle:LanguageTool(@"确定") otherButtonTitles:nil];
        [alert show];
        
    }

}

@end
